#ifndef SHARE
#define SHARE
 
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <vector>
#include <string>
#include<pcl/io/pcd_io.h>
#include<pcl/point_types.h>
using namespace std;
using namespace cv;
using PointT = pcl::PointXYZI;
 
namespace ShareMem{
    #define Shm_addrees 1203 //共享内存地址标识
    typedef struct ShareData
    {
        float time_stamp;
        int img_flag;
        char img_data[480*640*3];
        int img_pre_object_size;
        int img_pre_class[10];
        int  img_pre_2dbbox_xyxy[4*10];
    }  ShareData_;
 
 class ShareMemory{
    public:
            int shmid = shmget((key_t)Shm_addrees, sizeof(ShareData), 0666|IPC_CREAT);
            void *shm = shmat(shmid, (void*)0, 0);
            ShareData *p_share_data= (ShareData*)shm;
            int img_h=480;
            int img_w=640;
    public:
        ShareMemory(){ 
            p_share_data->img_flag = 0;
            printf("共享内存地址 ： %p\n", (int *)(shm));
         }
          
        ~ShareMemory() {
            cout<<"析构函数执行"<<endl;
            DestroyShare();
        }   
 
        void DestroyShare(){
            shmdt(shm);
            shmctl(shmid, IPC_RMID, 0);
            cout<<"共享内存已经销毁"<<endl;
        }

        void send_img(cv::Mat img){
            if(p_share_data->img_flag == 0){
                uchar *cvoutImg = (uchar*)img.data;
                memcpy(p_share_data->img_data, cvoutImg, img_h*img_w*3);
                p_share_data->img_flag = 1;
            }
        }
 
        uchar* get_img(){ 
            p_share_data->img_flag = 2;
            return (uchar*)p_share_data->img_data;
        }

        int get_img_flag(){
            return p_share_data->img_flag ;
        }

        void send_img_pre_data(int*  img_pre_class, int* img_pre_2dbbox, int img_pre_object_size){
            if(p_share_data->img_flag == 2){
                p_share_data->img_pre_object_size=img_pre_object_size;
                // memccpy(p_share_data->img_pre_class, img_pre_class, img_pre_object_size, sizeof(int));
                // memccpy(p_share_data->img_pre_2dbbox_xyxy, img_pre_2dbbox, img_pre_object_size*4, sizeof(int));

                for(int i = 0; i< img_pre_object_size; i++){
                    p_share_data->img_pre_class[i] = img_pre_class[i];
                }
                
                for(int i = 0; i< img_pre_object_size*4; i++){
                    p_share_data->img_pre_2dbbox_xyxy[i] = img_pre_2dbbox[i];
                }

                p_share_data->img_flag = 3;
            }
        }


     };//类定义结束
 
}//namespace 定义
 
 
//按照C语言格式重新打包-python调用
extern "C" {
    ShareMem::ShareMemory share_men_instance_;
    void DestroyShare_(){
         share_men_instance_.DestroyShare();
    }
 
    uchar* get_img(){
         share_men_instance_.get_img();
    }

    int get_img_flag(){
            share_men_instance_.get_img_flag();
    }

    void send_img_pre_data(int*  img_pre_class , int* img_pre_2dbbox, int img_pre_object_size){
        share_men_instance_.send_img_pre_data(img_pre_class,  img_pre_2dbbox,  img_pre_object_size);
    }
}
 
#endif
